/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lambkit.module.cms.web.tag;

import java.io.IOException;
import java.util.Map;

import com.lambkit.module.cms.rpc.model.CmsFile;
import com.lambkit.module.cms.rpc.api.CmsFileService;
import com.lambkit.common.util.StringUtils;
import com.lambkit.common.service.ServiceKit;
import com.lambkit.web.tag.LambkitTemplateModel;
import com.jfinal.kit.StrKit;
import com.jfinal.render.FreeMarkerRender;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2020-02-25
 * @version 1.0
 * @since 1.0
 */
/**
 * cms_file标签<br>
 * 参数：{id:主键}
 * 返回值：{entity:cms_file信息}
 * @author lambkit
 */
public class CmsFileMarker extends LambkitTemplateModel {

	@Override
	public void onRender(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		// TODO Auto-generated method stub
		String fileId = get(params, "file_id");
		String fileName = get(params, "file_name");
		String fileSize = get(params, "file_size");
		String fileExt = get(params, "file_ext");
		String fileUserId = get(params, "file_user_id");
		String fileUuid = get(params, "file_uuid");
		String filePath = get(params, "file_path");
		String fileState = get(params, "file_state");
		String fileCategoryId = get(params, "file_category_id");
		String fileTag = get(params, "file_tag");
		String fileCreateTime = get(params, "file_create_time");
		String fileBrief = get(params, "file_brief");
		int pagenum = getInt(params, "pagenum", 0);
		int pagesize = getInt(params, "pagesize", 0);
		String wheresql = get(params, "sql", null);
		String sql = " from cms_file where "; 
		if(wheresql == null) {
			sql += " 1=1 ";
			if(StringUtils.hasText(fileId)) sql += " and file_id=" + fileId;//int unsigned
			if(StringUtils.hasText(fileName)) sql += " and file_name like '%" + fileName + "%'";//varchar
			if(StringUtils.hasText(fileSize)) sql += " and file_size=" + fileSize;//int unsigned
			if(StringUtils.hasText(fileExt)) sql += " and file_ext like '%" + fileExt + "%'";//varchar
			if(StringUtils.hasText(fileUserId)) sql += " and file_user_id=" + fileUserId;//int unsigned
			if(StringUtils.hasText(fileUuid)) sql += " and file_uuid like '%" + fileUuid + "%'";//varchar
			if(StringUtils.hasText(filePath)) sql += " and file_path like '%" + filePath + "%'";//varchar
			if(StringUtils.hasText(fileState)) sql += " and file_state=" + fileState;//int
			if(StringUtils.hasText(fileCategoryId)) sql += " and file_category_id=" + fileCategoryId;//int unsigned
			if(StringUtils.hasText(fileTag)) sql += " and file_tag like '%" + fileTag + "%'";//varchar
			if(StringUtils.hasText(fileCreateTime)) sql += " and file_create_time like '%" + fileCreateTime + "%'";//datetime
			if(StringUtils.hasText(fileBrief)) sql += " and file_brief like '%" + fileBrief + "%'";//varchar
		} else {
			sql += wheresql;
		}
		
		String orderby = get(params, "orderby", null);
		
		if(StrKit.notBlank(orderby)) {
			sql += " order by " + orderby;
		}
		
		CmsFileService service = CmsFile.service();
		
		String tagEntityKeyname = get(params, "key", "entity");
		if(pagenum==0) {
			env.setVariable(tagEntityKeyname, FreeMarkerRender.getConfiguration().getObjectWrapper().wrap(
					service.dao().findFirst("select *" + sql)));
		} else {
			if(pagesize==0) {
				env.setVariable(tagEntityKeyname, FreeMarkerRender.getConfiguration().getObjectWrapper().wrap(
						service.dao().find("select *" + sql)));
			} else {
				env.setVariable(tagEntityKeyname, FreeMarkerRender.getConfiguration().getObjectWrapper().wrap(
						service.dao().paginate(pagenum, pagesize, "select *", sql)));
			}
		}
        body.render(env.getOut());
	}
}
